// 定义 promise 的三个状态
const PROMISE_STATUE_PENDING = 'pending' // 待定/悬而未决的
const PROMISE_STATUS_FULFILLED = 'fulfilled' // 处于fulfilled状态(已敲定/兑现状态)
const PROMISE_STATUS_REJECTED = 'rejected' // 处于rejected状态(已拒绝状态)

class HYPromise {
  constructor(executor) {
    this.status = PROMISE_STATUE_PENDING
    this.value = undefined
    this.reason = undefined

    const resolve = (value) => {
      if (this.status === PROMISE_STATUE_PENDING) {
        this.status = PROMISE_STATUS_FULFILLED
        this.value = value
        console.log("resolve 被调用")
      }
    }

    const reject = (value) => {
      if (this.status === PROMISE_STATUE_PENDING) {
        this.status = PROMISE_STATUS_REJECTED
        this.value = value
        console.log("reject 被调用")
      }
    }

    executor(resolve, reject)
  }
}

const promise = new HYPromise((resolve, reject) => {
  console.log("当前状态为 pending")
  resolve(111)
  reject(222)
})

promise.then(res => {}, err => {})